package com.jdy.haoduoai.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.model.LeSysMessage;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.rx.util.async.Async;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.HashMap;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

//import com.jdy.haoduoai.R;

@SuppressLint("SetJavaScriptEnabled")
public class MessageDetailsActivity extends BaseActivity {

  @InjectView(id = R.id.content)
  private TextView content;
  @InjectView(id = R.id.title)
  private TextView title;
  @InjectView(id = R.id.time)
  private TextView time;
  @InjectView(id = R.id.main)
  private LinearLayout main;

  private LeSysMessage item;

  @Override
  @SuppressLint("InflateParams")
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messagedetails);
    setTitle("消息详情");
    initialize();
    initialView();
    initialListener();
    requestDatas();
  }

  private void initialize() {
    Intent intent = getIntent();
    item = (LeSysMessage) intent.getSerializableExtra("MessageItem");
    title.setText(item.title);
    time.setText(item.created_at);
    content.setText(item.content);
  }

  private void initialView() {

  }

  private void initialListener() {
  }

  private void requestDatas() {
    Async
        .start(new Func0<ResponseResult>() {
          @Override
          public ResponseResult call() {
            LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
            HashMap<String, Object> param_ = new HashMap<String, Object>();
            param_.put(HttpUtils.TAG_SNO_I, lu.sno);
            param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

            return HttpUtils.startRequest(HttpUtils.API_URL
                + "/api/2.0/member/message/" + item.id, param_,
                HttpUtils.TAG_GET_REQUEST);

          }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .filter(new Func1<ResponseResult, Boolean>() {

          @Override
          public Boolean call(ResponseResult arg0) {

            boolean r = (arg0 != null && Tools.isNotEmpty(arg0) && arg0
                .isSuccess());
            // if (r) {
            // toastShow("删除信息成功");
            // }
            return r;
          }
        }).doOnCompleted(new Action0() {

          @Override
          public void call() {

          }
        }).subscribe(new Action1<ResponseResult>() {

          @Override
          public void call(ResponseResult arg0) {

            if (arg0.isSuccess()) {
              // toastShow("删除信息成功");
            }

          }
        });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

}
